<?php

App::uses('AppController', 'Controller');

/**
 * Newsletters Controller
 *
 * @property Newsletter $Newsletter
 * @property PaginatorComponent $Paginator
 */
class NewslettersController extends NewslettersAppController {

    /**
     * __construct
     *
     * @var mixed
     * @var mixed
     */
    public function __construct($request = null, $response = null) {
        parent::__construct($request, $response);
        $this->set('title', __d('newsletters', 'NEWSLETTERS'));
    }

    /**
     * Components
     *
     * @var array
     */
    public $components = array('Paginator');

    /* --------------------------------------     Public    ----------------------------------------- */

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->Newsletter->create();
            if ($this->Newsletter->save($this->request->data)) {
                $this->Session->setFlash(__('The %s has been saved.', Inflector::humanize($this->request->params['controller'])), 'alert', array(
                    'plugin' => 'BoostCake',
                    'class' => 'alert-success'
                ));
            } else {
                $this->Session->setFlash(__('The %s could not be saved. Please, try again.', Inflector::humanize($this->request->params['controller'])), 'alert', array(
                    'plugin' => 'BoostCake',
                    'class' => 'alert-danger'
                ));
            }
        }
        return $this->redirect($this->referer());
    }

    /* --------------------------------------     Admin    ----------------------------------------- */

    /**
     * index method
     *
     * @return void
     */
    public function admin_index() {
        $this->Newsletter->recursive = 0;
        $this->set('newsletters', $this->Paginator->paginate());
        $this->set('title', 'Newsletter - Consultar');
    }

    /**
     * admin_view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_view($id = null) {
        if (!$this->Newsletter->exists($id)) {
            throw new NotFoundException(__('Invalid %s', Inflector::humanize($this->request->params['controller'])));
        }
        $options = array('conditions' => array('Newsletter.' . $this->Newsletter->primaryKey => $id));
        $this->set('newsletter', $this->Newsletter->find('first', $options));
        $this->set('title', 'Newsletter - Detalhes');
    }

    /**
     * admin_add method
     *
     * @return void
     */
    public function admin_add() {
        if ($this->request->is('post')) {
            $this->Newsletter->create();
            if ($this->Newsletter->save($this->request->data)) {
                $this->Session->setFlash(__('The %s has been saved.', Inflector::humanize($this->request->params['controller'])), 'alert', array(
                    'plugin' => 'BoostCake',
                    'class' => 'alert-success'
                ));
                return $this->redirect(array('action' => 'edit', $this->Newsletter->id));
            } else {
                $this->Session->setFlash(__('The %s could not be saved. Please, try again.', Inflector::humanize($this->request->params['controller'])), 'alert', array(
                    'plugin' => 'BoostCake',
                    'class' => 'alert-danger'
                ));
            }
        }
        $this->set('title', 'Newsletter - Cadastrar');
    }

    /**
     * admin_edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        if (!$this->Newsletter->exists($id)) {
            throw new NotFoundException(__('Invalid %s', Inflector::humanize($this->request->params['controller'])));
        }
        if ($this->request->is(array('post', 'put'))) {
            if ($this->Newsletter->save($this->request->data)) {
                $this->Session->setFlash(__('The %s has been saved.', Inflector::humanize($this->request->params['controller'])), 'alert', array(
                    'plugin' => 'BoostCake',
                    'class' => 'alert-success'
                ));
                return $this->redirect($this->referer());
            } else {
                $this->Session->setFlash(__('The %s could not be saved. Please, try again.', Inflector::humanize($this->request->params['controller'])), 'alert', array(
                    'plugin' => 'BoostCake',
                    'class' => 'alert-danger'
                ));
            }
        } else {
            $options = array('conditions' => array('Newsletter.' . $this->Newsletter->primaryKey => $id));
            $this->request->data = $this->Newsletter->find('first', $options);
        }
        $this->set('title', 'Newsletter - Editar');
    }

    /**
     * admin_delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null) {
        $this->Newsletter->id = $id;
        if (!$this->Newsletter->exists()) {
            throw new NotFoundException(__('Invalid %s', Inflector::humanize($this->request->params['controller'])));
        }
        $this->request->allowMethod('post', 'delete');
        if ($this->Newsletter->delete()) {
            $this->Session->setFlash(__('The %s has been deleted.', Inflector::humanize($this->request->params['controller'])), 'alert', array(
                'plugin' => 'BoostCake',
                'class' => 'alert-success'
            ));
        } else {
            $this->Session->setFlash(__('The %s could not be deleted. Please, try again.', Inflector::humanize($this->request->params['controller'])), 'alert', array(
                'plugin' => 'BoostCake',
                'class' => 'alert-danger'
            ));
        }
        return $this->redirect($this->referer());
    }

}
